package com.hkemmm.backstage.controller;

import com.hkemmm.shop.entity.Goods;
import com.hkemmm.shop.entity.Order;
import com.hkemmm.shop.entity.OrderInfo;
import com.hkemmm.shop.service.IGoodsService;
import com.hkemmm.shop.service.IOrderService;
import com.hkemmm.shop.service.UserService;
import com.hkemmm.shop.utlis.DateTypeConverter;
import com.hkemmm.shop.utlis.UploadFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/back")
public class BackStage {
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IOrderService orderService;
    @Autowired
    private UserService userService;


    @ResponseBody
    @RequestMapping("/allGoods")
    public Object getAllGoods(Integer shopId) {
        List<Goods> goods = goodsService.getAllGoods(shopId);

        return goods;

    }

    @RequestMapping(value = "/saveAndUpdate", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String updateGoods(Goods goods, MultipartFile file) {
        if (file != null) {
            String fileName = UploadFile.uploadFile(file, "E:\\home\\upload\\");
            goods.setImg(fileName);
        }

        if (goods.getId() == null) {
            goodsService.insertSelective(goods);
        } else {
            goodsService.updateByPrimaryKeySelective(goods);
        }
        return "ok";
    }

    @RequestMapping("/delGoods")
    @ResponseBody
    public String delGoods(Integer id, Integer status) {

//        System.out.println(id);
//        System.out.println(status);
        goodsService.delGoods(id, status);

        return "ok";
    }

    @RequestMapping("/allOrders")
    @ResponseBody
    public Object getAllOrder() {
        List<Order> orders = orderService.getAllOrder();

//        for (Order o : orders) {
//            o.setAddtime(new DateTypeConverter().timeStamp2Date(o.getAddtime().toString()));
//        }

        return orders;
    }


    @RequestMapping("/allUser")
    @ResponseBody
    public Object getAllUser() {
        return userService.getAllUser();
    }

}
